package com.zline.foodraise.ui.activity;

import android.view.View;
import android.widget.TextView;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.zline.foodraise.R;
import com.zline.foodraise.constant.Constant;
import com.zline.foodraise.http.HttpErro;
import com.zline.foodraise.http.HttpOperator;
import com.zline.foodraise.http.HttpUtil;
import com.zline.foodraise.util.LUtil;
import com.zline.foodraise.util.NetworkUtil;
import com.zline.foodraise.util.TUtil;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.res.StringRes;
import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import java.math.BigDecimal;

/**
 * Created by heshantao on 15/7/1.
 * 我的钱包
 */
@EActivity(R.layout.activity_wallet)
public class WalletActivity extends ZlineActivity {
    private static final String LOG_TAG = WalletActivity.class.getSimpleName();
    //标题
    @StringRes(R.string.my_wallet)
    String title;
    @ViewById(R.id.app_common_header_title_tv)
    TextView title_tv;
    @ViewById(R.id.red_wallet_balance_tv)
    TextView red_wallet_balance_tv;
    @ViewById(R.id.account_balance_tv)
    TextView account_balance_tv;
    @Bean
    HttpOperator httpOperator;
    @Bean
    HttpUtil httpUtil;
    int resultCode = 0;
    @Bean
    HttpErro httpErro;
    //筹红包越
    double red_wallet_balance;
    //账户余额
    double account_balance;
    @Bean
    NetworkUtil networkUtil;

    @AfterViews
    void doSomeAfterViews() {
        initView();
    }

    void initView() {
        title_tv.setText(title);
        if (networkUtil.isNetworkAvailable()) {
            showLoadingDialog();
            setRequestWalletBalance();
        } else {
            TUtil.showShort(this, R.string.fail_load_data);
        }
    }


    void setRequestWalletBalance() {
        httpOperator.requstWalletBalance(httpUtil, requestWalletBalanceHandler);
    }

    //获取用户账户信息回调
    AsyncHttpResponseHandler requestWalletBalanceHandler = new JsonHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
            super.onSuccess(statusCode, headers, response);
            LUtil.i(LOG_TAG, "____successs_____requestWalletBalanceHandler_____" + statusCode + response.toString());
            if (statusCode == 200) {
                try {
                    resultCode = response.getInt(Constant.RESULT_CODE);
                    if (resultCode == 200) {//调用成功
                        red_wallet_balance = response.getDouble(Constant.RED_WALLET_BALANCE);
                        account_balance=response.getDouble(Constant.ACCOUNT_BALANCE);
                        setBalance(red_wallet_balance,account_balance);
                    } else {
                        httpErro.showCommonErroResult(resultCode);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                } finally {
                    dissLoadingDialog();
                }
            }
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
            super.onFailure(statusCode, headers, responseString, throwable);
            dissLoadingDialog();
        }
    };

    //设置筹钱包和账户余额
    void setBalance(double red_wallet_balance,double account_balance) {
        //小数点保留2位小数
        BigDecimal bd = new BigDecimal(red_wallet_balance);
        BigDecimal bd2 = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
        red_wallet_balance_tv.setText("￥"+bd2);
        bd=new BigDecimal(account_balance);
        bd2 = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
        account_balance_tv.setText("￥"+bd2);
    }

    @Click({R.id.app_common_header_left_icon_iv, R.id.red_wallet_rl,R.id.balance_rl})
    void OnClick(View view) {
        switch (view.getId()) {
            case R.id.app_common_header_left_icon_iv:
                goback();
                break;
            case R.id.red_wallet_rl:
                TradingDetailActivity_.intent(this).type(1).start();
                break;
            case R.id.balance_rl:
                TradingDetailActivity_.intent(this).type(2).start();
                break;
            default:
                break;
        }
    }

}
